<?php
namespace app\farm\model;

use app\BaseModel;
use think\facade\Db;

class ClaimCollage extends BaseModel
{
    //定义表名
    protected $name = 'lbfarm_v2_claim_collage_list';


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){

        $data['create_time'] = time();

        $res = $this->insert($data);

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page){

        $data = $this->where($dis)->order('top desc,id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2022-07-01 15:33
     * @功能说明:
     */
    public function adminDataList($dis,$page=10){

        $data = $this->alias('a')
                ->join('lbfarm_claim b','a.claim_id = b.id','left')
                ->where($dis)
                ->field('a.*,b.title as claim_title,b.price as init_price,b.cover as claim_cover')
                ->group('a.id')
                ->order('a.top desc,a.id')
                ->paginate($page)
                ->toArray();

        return $data;


    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2022-08-18 10:33
     * @功能说明:
     */
    public function getAtvInfo($atv_id,$claim_id){

        $data = $this->dataInfo(['id'=>$atv_id,'claim_id'=>$claim_id]);

        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2022-08-18 11:16
     * @功能说明:
     */
    public function getAtvInfoIng($claim_id,$user_id=0){

        $dis[] = ['claim_id','=',$claim_id];

        $dis[] = ['start_time','<',time()];

        $dis[] = ['end_time','>',time()];

        $dis[] = ['status','=',1];

        $data = $this->dataInfo($dis);

        if(!empty($data)){

            $collage_model = new CollageStart();

            $data['can_start'] = $collage_model->startAtvCheck($user_id,$data['id']);
            //是否还能发起该活动其他的拼团
            $data['can_start'] = !empty($data['can_start']['code'])?0:1;
        }

        return $data;
    }







}